package com.woniuxy.shop.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.shop.entity.User;
import com.woniuxy.shop.service.CartService;
import com.woniuxy.shop.service.UserService;
import com.woniuxy.shop.utils.CookieUtils;
import com.woniuxy.shop.utils.JwtUtils;
import com.woniuxy.shop.vo.CartItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fanqiwen
 * @since 2023-02-07 11:56:02
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    UserService userService;

    @RequestMapping("addToCart")
    @ResponseBody
    public String addToCart(Integer prodectId, HttpServletRequest request) {
        User currentUser = getCurrentUser(request);
        cartService.addToCart(prodectId, currentUser.getId());
        return "ok";
    }

    @RequestMapping("getAll")
    @ResponseBody
    public List<CartItemVo> getAll(HttpServletRequest request) {
        User currentUser = getCurrentUser(request);
        return cartService.getAll(currentUser.getId());
    }
    @RequestMapping("updateBuyCount")
    @ResponseBody
    public String updateBuyCount(Integer productId,Integer changeNum, HttpServletRequest request) {
        User currentUser = getCurrentUser(request);
        try {
            cartService.updateBuyCount(productId,changeNum,currentUser.getId());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "ok";
    }
    @RequestMapping("calTotalPrice")
    @ResponseBody
    public BigDecimal calTotalPrice(@RequestParam("productIds[]")Integer[] productIds,HttpServletRequest request){
        //post请求 不能绑定前端数组参数需要用@RequestParam get请求可以直接绑定
        User currentUser = getCurrentUser(request);
        BigDecimal totalPrice=cartService.calTotalPrice(currentUser.getId(),productIds);
        return totalPrice;
    }
    @RequestMapping("toOrderPreview")
    public String toOrderPreview(Integer[] productIds, Model model) throws JsonProcessingException {
        //post请求 不能绑定前端数组参数需要用@RequestParam get请求可以直接绑定
        //传给前端页面的时候，Java对象要转换为json字符串，才能在js中识别
        model.addAttribute("productIds",new ObjectMapper().writeValueAsString(productIds));
        return "orderPreview";
    }
    private User getCurrentUser(HttpServletRequest request) {
        String token = CookieUtils.getUserToken2Cookie(request);
        String account = JwtUtils.getAccount(token);
        return userService.getUserByName(account);
    }
}

